﻿@{
    // 带看反馈
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<script>
    var orderId = $.request("orderId");
    var formId = $.request("formId");
    var reason = [];

    function find_checkbox(success, error) {
        var $gridList = this;
        var checkboxs = $gridList.find("input[type='checkbox']:checked"),
            selectedVals = [];
        if (checkboxs.length) {
            checkboxs.each(function (n, ele) { selectedVals.push(ele.value); });
            success && success(selectedVals);
        } else {
            error && error();
        }
    }

    function valid_checkbox() {
        var args = Array.prototype.slice.call(arguments);
        if (args.length == 1) {
            args.push(function () {
                $.modalAlert("请选择相关的订单项", "success");
            });
        }
        find_checkbox.apply(this, args)
    }

    function bind_reasondata() {
        var html = '';
        if (reason && reason.length) {
            for (var i = 0; i < reason.length; i++) {
                var item = reason[i];
                html += '<option value="' + item.Value + '">';
                html += item.Text;
                html += '</option>';
            }
            return html;
        }
    }

    $(function () {
        $.ajax({
            url: "/OrderFlowManage/ClientOrder/GetItemJson?enCode=dkfk",
            data: {},
            dataType: "json",
            async: false,
            success: function (reasonData) {
                reason = reasonData;
                $.ajax({
                    url: "/OrderFlowManage/ClientOrder/GetTakelookFeelbackJson",
                    data: { orderId: orderId },
                    dataType: "json",
                    async: false,
                    success: function (data) {
                        var index = 1;
                        var $previewTakelookGrid = $('#previewTakelookGrid');
                        var html = '',
                            first = '',
                            allState = ["未审核", "通过", "驳回"],
                            allStateColor = ["<button class='btn btn-xs btn-default' type='button' disabled>", "<button class='btn btn-xs btn-success' type='button' disabled>", "<button class='btn btn-xs btn-danger' type='button' disabled>"];

                        for (var i in data.rows) {
                            var item = data.rows[i];
                            html += '<tr id="tr' + index + '">';
                            html += '<td>';
                            if (first != item.F_No) {
                                if (item.F_State == 1) {
                                    html += '<input type="checkbox" value="' + item.F_SiteId + '|' + item.F_SpaceId + '|' + item.F_No  + '">';
                                }
                                else {
                                    html += '<input type="checkbox" disabled value="' + item.F_SiteId + '|' + item.F_SpaceId + '|' + item.F_No  + '">';
                                }
                            }
                            html += '</td>';
                            html += '<td>';
                            if (first != item.F_No) {
                                html += item.F_No;
                                first = item.F_No;
                            }
                            html += '</td>';
                            html += '<td>';
                            if (item.F_State == 1) {
                                html += '<select name="F_ServiceCode" type="text" class="form-control required F_ServiceCode">';
                                html += bind_reasondata();
                                html += '</select>';
                            }
                            else {
                                html += '<select name="F_ServiceCode" type="text" class="form-control F_ServiceCode" disabled></select>';
                            }
                   
                            html += '</td>';
                            html += '<td class="formValue">';
                            if (item.F_State == 1) {
                                html += '<textarea name="F_Remark" rows="1" class="form-control required" style="width:80%;"></textarea>';
                            }
                            else {
                                html += '<textarea name="F_Remark" rows="1" class="form-control" style="width:80%;" disabled></textarea>';
                            }
                            html += '</td>';
                            html += '<td>';
                            html += item.SiteName + '-' + item.SpaceName;
                            html += '</td>';
                            html += '<td>';
                            html += $.map(JSON.parse(item.Times), function (n) { return n.startTime + '至' + n.endTime; }).join('<br >');
                            html += '</td>';
                            html += '<td>';
                            html += $.map(JSON.parse(item.Cars), function (n) { return n.resName + '车牌号：' + n.carName + '司机：' + n.driverName; }).join('<br >');
                            html += '</td>';
                            html += '<td>';
                            html += $.map(JSON.parse(item.Users), function (n) { return n.deptName + ' ' + n.userName; }).join('<br >');
                            html += '</td>';
                            html += '<td>';
                            html += (allStateColor[item.F_State || 0]);
                            html += (allState[item.F_State || 0]);
                            html += ("</button>");
                            html += '</td>';

                            html += '</tr>';

                            index++;
                        }
                        $previewTakelookGrid.html(html);
                        $(".F_ServiceCode").bindSelect();
                    }
                });
            }
        });
    });

    function submitForm() {
        var $previewTakelookGrid = $('#previewTakelookGrid');
        var trs = $previewTakelookGrid.find('tr');
        
        valid_checkbox.call($previewTakelookGrid, function (cks) {
            if (cks.length) {
                var param = {
                    orderId: orderId,
                    flowFormId: formId,
                    No: cks[0].split("|")[2],
                    data: []
                };

                trs.each(function (i, ele) {
                    var ck = $(this).find("input[type='checkbox']").val();
                    var ckchecked = $(this).find("input[type='checkbox']").prop("checked");
                    var selected = $(this).find("select").val();
                    var textarea = $(this).find("textarea").val();
                    for (var j = 0; j < cks.length; j++) {
                        if (ck == cks[j] && ckchecked) {
                            param.data.push({
                                SiteIdAndSpaceId: ck,
                                F_Reason: selected,
                                F_Remark: textarea
                            });
                        }
                    }
                });
                console.log(param.data);

                $.submitForm({
                    url: "/OrderFlowManage/ClientOrder/SubmitTakelookFeelbackForm?keyValue=" + orderId,
                    param: param,
                    success: function () {
                        //$.currentWindow().vm.takkelook.initTakelook();
                        // Index页面和Detail页面都有调用相关的方法，要确认相关
                        $.currentWindow().$("#btn_takelook").prop("disabled", true);
                        $.currentWindow().reloadPage(3);
                        
                        $.loading(false);
                    }
                });
            } else {
                $.modalMsg("一次最多只能申请一个批次的场地申请", "success");
            }
            
        });
    }
</script>

<form id="form1">
    <div style="margin:20px;">
        <table class="form">
            <tr>
                <td>
                    <table class="table table-condensed table-bordered">
                        <thead>
                            <tr>
                                <td width="30">序号</td>
                                <td>批次号</td>
                                <td>原因</td>
                                <td>备注</td>
                                <td>带看场地</td>
                                <td>带看时间</td>
                                <td>预约车辆</td>
                                <td>预约陪同人员</td>
                                <td>状态</td>
                            </tr>
                        </thead>
                        <tbody id="previewTakelookGrid"></tbody>
                    </table>
                </td>
            </tr>
        </table>
    </div>
</form>


